clearField = function(elem) {
	elem.value="";
	elem.setStyle({
		color:'#250'
	});
}
restoreField = function(elem, str) {
	if(elem.value=="" || elem.value==str) {
		elem.value=str;
	}
	elem.setStyle({
		color:'#bfbfbf'
	});
}
magnify = function(elem) {
	new Effect.Morph(elem.childElements()[0], {
		style:'width:40px; height:40px;z-index:99;'
	});
}
deMagnify = function(elem) {
	new Effect.Morph(elem.childElements()[0], {
		style:'width:15px; height:15px;'
	});
}
goToURL = function(e, base_url,  type, elem) {
	var intKey = 0;
	e = (window.event)? event : e;
	intKey = (e.keyCode)? e.keyCode: e.charCode;
	if(elem.value!="" && intKey==13) {
		location.href = base_url+"search/"+type+":"+elem.value;
	}
}


var instanceNo = 0;
var AjaxProgressBar = Class.create();
AjaxProgressBar.prototype = {
	initialize: function(progressBarElementId, barElementId, initProperties){
		this.progressBarElement = $(progressBarElementId);
		this.barElement = $(barElementId);		
		if (initProperties!=null) {
			this.frequency = initProperties["frequency"];
			this.url = initProperties["url"];
			this.total = initProperties["total"];
		} else {
			this.frequency = 2;
		}
		this.value = 0;
		this.isFinish = false;
		this.repaint();
		this.id = "AjaxProgressBar"+ instanceNo++;
		window[this.id] = this; 
	},
	setValue: function(value){ this.value = value; this.repaint(); },
	repaint: function() {
		if (this.value>=0 && this.value<=100) {
			this.barElement.style.width = this.value +"%";
		}
	},
	start: function() {
		this.update();
	},
	update: function() {
		var upThis = this;
		var ajax = new Ajax.Request(this.url, {
			method: "get",
			onSuccess: function(res) {
				upThis.updateSuccess(res);
			},
			onFailure: function(res) {
				$('footer').innerHTML = ("Error: "+ res.status +" "+ res.statusText);
			}
		});
		if(!this.isFinish) {
			setTimeout("window."+ this.id +".update()", this.frequency*1000);
		}
	},
	updateSuccess: function(res) {
		var value = parseInt(res.responseXML.getElementsByTagName("value")[0].firstChild.nodeValue);
		var message = res.responseXML.getElementsByTagName("message")[0].firstChild.nodeValue;
		var percent = Math.floor((100*value)/this.total);
		this.setValue(percent);
		this.barElement.innerHTML = message;
		this.isFinish = percent == 100;
	}
}





var instanceNo = 0;
var ProgressBar = Class.create();
ProgressBar.prototype = {
	initialize: function(progressBarElementId, barElementId, initProperties){
		this.progressBarElement = $(progressBarElementId);
		this.barElement = $(barElementId);		
		if (initProperties!=null) {
			this.frequency = initProperties["frequency"];
			this.timestamp = initProperties["timestamp"];
			this.last = initProperties["last"];
			this.timeout = initProperties["timeout"];
			this.total = initProperties["total"];
			this.adds = initProperties["adds"];
		} else {
			this.frequency = 2;
		}
		this.value = 0;
		this.isFinish = false;
		this.repaint();
		this.id = "AjaxProgressBar"+ instanceNo++;
		window[this.id] = this; 
	},
	setValue: function(value){ this.value = value; this.repaint(); },
	repaint: function() {
		if (this.value>=0 && this.value<=100) {
			$(this.barElement).setStyle({
				width:this.value +"%"
			});
			//this.barElement.style.width = this.value +"%";
			//$(this.barElement).morph('width:'+this.value+'%');
		}
	},
	start: function() {
		if(this.adds<0) {
			this.update();
		} else {
			tags = (this.adds!=0)? this.adds : "No";
			$(this.barElement).innerHTML = "<b>"+tags+"</b> tags added";
		}
	},
	update: function() {
		var barWidth = 500*((this.timestamp-this.last)/this.timeout);
		var toWait = (this.timeout-(this.timestamp-this.last));
		var msg = "";
		if(toWait>60) {
			msg = Math.floor(toWait/60)+" minute";
			if(Math.floor(toWait/60)>1) {
				msg += "s"
			}
			msg += " and ";
			toWait = toWait%60;
		}
		msg += toWait+" seconds"
		$(this.barElement).innerHTML = "Wait "+msg+" to update again";
		var percent = Math.floor((100*barWidth)/this.total);
		this.setValue(percent);
		this.timestamp = this.timestamp+this.frequency;
		this.isFinish = percent == 100;
		if(!this.isFinish) {
			setTimeout("window."+ this.id +".update()", this.frequency*1000);
		} else {
			$(this.barElement).innerHTML = "You can refresh the page now";
			new Effect.Highlight(this.progressBarElement, {
				duration:2
			});
		}
	}
}